package ihm;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;

import commands.Command;
import constants.Constants;

/**
 * Implémentation de l'interface ApplicationPanel, basée sur la bibliothèque Swing.
 */
public class ApplicationPanelImpl extends JPanel implements ApplicationPanel{

	private static final long serialVersionUID = 6948772887983460171L;
	
	private JLabel sliderLabel;
	private JSlider slider;	// Slider représentant le capteur
	private JLabel sliderValueLabel;
	private Command notifyChangesToSensorCommand;

	private ButtonGroup strategiesButtonGroup;
	private JRadioButton atomicStrategyRadioButton;
	private JRadioButton sequentialStrategyRadioButton;
	private JRadioButton epochStrategyRadioButton;
	private int currentStrategyNumber;
	private Command notifyStrategyChangesToSensorCommand;
	
	private int nbDisplayerPanel;

	public ApplicationPanelImpl() {	
		super();

		setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		
		// Partie capteur
		sliderLabel = new JLabel("Capteur : ");
		constraints.gridx = 0;
		constraints.gridy = 1;
		add(sliderLabel, constraints);

		slider = new JSlider();
		slider.setMinimum(Constants.SENSOR_MIN_VALUE);
		slider.setMaximum(Constants.SENSOR_MAX_VALUE);
		slider.setValue(Constants.SENSOR_DEFAULT_VALUE);
		slider.addMouseListener(new SliderMouseListener());
		constraints = new GridBagConstraints();
		constraints.gridx = 1;
		constraints.gridy = 1;
		//constraints.gridwidth = 2;
		add(slider, constraints);
		
		sliderValueLabel = new JLabel("" + Constants.SENSOR_DEFAULT_VALUE);
		constraints = new GridBagConstraints();
		constraints.gridx = 2;
		constraints.gridy = 1;
		add(sliderValueLabel, constraints);
		
		// Partie stratégies de diffusion
		strategiesButtonGroup = new ButtonGroup();
		
		atomicStrategyRadioButton = new JRadioButton("Atomique");
		atomicStrategyRadioButton.addActionListener(new StrategyActionListener(Constants.ATOMIC_STRATEGY));
		constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = 4;
		add(atomicStrategyRadioButton, constraints);

		sequentialStrategyRadioButton = new JRadioButton("Sequentiel");
		sequentialStrategyRadioButton.addActionListener(new StrategyActionListener(Constants.SEQUENTIAL_STRATEGY));
		constraints = new GridBagConstraints();
		constraints.gridx = 1;
		constraints.gridy = 4;
		add(sequentialStrategyRadioButton, constraints);

		epochStrategyRadioButton = new JRadioButton("Par époque");
		epochStrategyRadioButton.addActionListener(new StrategyActionListener(Constants.EPOCH_STRATEGY));
		constraints = new GridBagConstraints();
		constraints.gridx = 2;
		constraints.gridy = 4;
		add(epochStrategyRadioButton, constraints);

		strategiesButtonGroup.add(atomicStrategyRadioButton);
		strategiesButtonGroup.add(sequentialStrategyRadioButton);
		strategiesButtonGroup.add(epochStrategyRadioButton);
	
		currentStrategyNumber = Constants.STRATEGY_DEFAULT;
		atomicStrategyRadioButton.setSelected(true);
	}
	
	@Override
	public void addDisplayerPanel(DisplayerPanel displayerPanel) {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridwidth = 3;
		constraints.gridy = 5 + nbDisplayerPanel;
		add((JPanel)displayerPanel, constraints);
		nbDisplayerPanel++;
	}
	
	public void addSensorModifierActivationPanel(SensorModifierActivationPanel sensorPanel){
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridwidth = 3;
		constraints.gridy = 0;
		add((JPanel)sensorPanel, constraints);
	}

	@Override
	public int getValue() {
		return slider.getValue();
	}
	
	@Override
	public void setValue(int value) {
		slider.setValue(value);
		sliderValueLabel.setText("" + slider.getValue());
	}

	@Override
	public void removeDisplayerPanel(DisplayerPanel displayerPanel) {
		remove((JPanel)displayerPanel);
	}

	@Override
	public void setNotifyChangesToSensorCommand(Command notifyChangesToSensorCommand) {
		this.notifyChangesToSensorCommand = notifyChangesToSensorCommand;
	}
	
	@Override
	public void enableSensorModification(boolean enabled) {
		slider.setEnabled(enabled);
	}
	
	@Override
	public void setNotifyStrategyChangesToSensorCommand(Command notifyStrategyChangesToSensorCommand) {
		this.notifyStrategyChangesToSensorCommand = notifyStrategyChangesToSensorCommand;
	}
	
	@Override
	public int getCurrentStrategyNumber() {
		return currentStrategyNumber;
	}
	
	class SliderMouseListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			notifyChangesToSensorCommand.execute();
		}
		
	}
	
	class StrategyActionListener implements ActionListener {
				
		private int strategyNumber;
		
		public StrategyActionListener(int strategyNumber) {
			this.strategyNumber = strategyNumber;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			currentStrategyNumber = strategyNumber;
			notifyStrategyChangesToSensorCommand.execute();
		}
		
	}

}
